package org.dromara.learning_xq.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.dromara.learning_xq.entity.AsFormativeTest;
import org.dromara.learning_xq.entity.dto.AsFormativeTestDto;
import org.dromara.learning_xq.service.AsFormativeTestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("asFormativeTest")
@Tag(name = "模块")
public class AsFormativeTestController {

    @Autowired
    private AsFormativeTestService asFormativeTestService;


    @PostMapping("insertAsFormativeTests")
    @Operation(summary = "批量新增模块")
/**
 * 批量新增模块信息。
 *
 * @param asFormativeTestDtoList 包含多个模块信息的DTO列表，用于批量添加。
 * @return 返回一个包含状态和数据的Map对象，用于前端展示添加结果。
 */
    public ResponseEntity<Map<String, Object>> insertAsFormativeTests(@RequestBody List<AsFormativeTestDto> asFormativeTestDtoList) {
        try {
            // 调用服务层方法处理新增操作
            List<AsFormativeTestDto> resultList = asFormativeTestService.increaseAsFormativeTest(asFormativeTestDtoList);

            // 构建成功响应
            Map<String, Object> response = new HashMap<>();
            response.put("status", "success");
            response.put("data", "添加成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            // 构建失败响应
            Map<String, Object> response = new HashMap<>();
            response.put("status", "failure");
            response.put("message", "添加失败"); // 捕获并返回具体的错误信息
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }


    @PostMapping("updateAsFormativeTest")
    @Operation(summary = "批量更新模块")
// 批量更新AsFormativeTest信息
    public ResponseEntity<Map<String, Object>> updateAsFormativeTest(@RequestBody List<AsFormativeTestDto> asFormativeTestDtoList) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 调用服务层方法处理更新操作
            List<AsFormativeTestDto> resultList = asFormativeTestService.updateAsFormativeTest(asFormativeTestDtoList);

            // 构建成功响应
            response.put("status", "success");
            response.put("data", resultList); // 返回更新后的数据列表
            response.put("message", "更新成功");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            // 构建错误响应
            response.put("status", "error");
            response.put("message", "更新失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @PostMapping("selverAll")
    @Operation(summary = "查询所有模块")
    public ResponseEntity<Map<String, Object>> selverAll(@RequestBody AsFormativeTestDto asFormativeTestDto) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 调用服务层方法，获取查询结果
            List<AsFormativeTestDto> resultList = asFormativeTestService.selverAll(asFormativeTestDto);

            // 判断查询结果是否为空
            if (resultList != null && !resultList.isEmpty()) {
                response.put("status", "success");
                response.put("message", "查询成功");
                response.put("data", resultList);
            } else {
                response.put("status", "success");
                response.put("message", "查询结果为空");
                response.put("data", resultList); // 可以根据需要返回空列表
            }
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            response.put("status", "error");
            response.put("message", "查询失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    @PostMapping("removeBatchByIds")
    @Operation(summary = "批量删除模块")
    public ResponseEntity<Map<String, Object>> removeBatchByIds(@RequestBody List<Long> ids) {
        Map<String, Object> response = new HashMap<>();
        try {
            // 调用服务层方法，删除指定ID的模块
            boolean result = asFormativeTestService.removeBatchByIds(ids);

            // 根据删除结果构建响应
            if (result) {
                response.put("status", "success");
                response.put("message", "删除成功");
            } else {
                response.put("status", "failure");
                response.put("message", "删除失败或未找到对应的记录");
            }
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            // 构建异常情况下的响应
            response.put("status", "error");
            response.put("message", "删除失败: " + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }


}
